

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>26.5. hotshot — High performance logging profiler &mdash; Python v2.7.8 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.8',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7.8 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7.8 documentation" href="../index.html" />
    <link rel="up" title="26. Debugging and Profiling" href="debug.html" />
    <link rel="next" title="26.6. timeit — Measure execution time of small code snippets" href="timeit.html" />
    <link rel="prev" title="26.4. The Python Profilers" href="profile.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="timeit.html" title="26.6. timeit — Measure execution time of small code snippets"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="profile.html" title="26.4. The Python Profilers"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="debug.html" accesskey="U">26. Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-hotshot">
<span id="hotshot-high-performance-logging-profiler"></span><h1>26.5. <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><tt class="xref py py-mod docutils literal"><span class="pre">hotshot</span></tt></a> &#8212; High performance logging profiler<a class="headerlink" href="#module-hotshot" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.</span></p>
<p>This module provides a nicer interface to the <tt class="xref py py-mod docutils literal"><span class="pre">_hotshot</span></tt> C module. Hotshot
is a replacement for the existing <a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><tt class="xref py py-mod docutils literal"><span class="pre">profile</span></tt></a> module. As it&#8217;s written mostly
in C, it should result in a much smaller performance impact than the existing
<a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><tt class="xref py py-mod docutils literal"><span class="pre">profile</span></tt></a> module.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><tt class="xref py py-mod docutils literal"><span class="pre">hotshot</span></tt></a> module focuses on minimizing the overhead while profiling, at
the expense of long data post-processing times. For common usage it is
recommended to use <a class="reference internal" href="profile.html#module-cProfile" title="cProfile"><tt class="xref py py-mod docutils literal"><span class="pre">cProfile</span></tt></a> instead. <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><tt class="xref py py-mod docutils literal"><span class="pre">hotshot</span></tt></a> is not maintained and
might be removed from the standard library in the future.</p>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.5: </span>The results should be more meaningful than in the past: the timing core
contained a critical bug.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#module-hotshot" title="hotshot: High performance logging profiler, mostly written in C."><tt class="xref py py-mod docutils literal"><span class="pre">hotshot</span></tt></a> profiler does not yet work well with threads. It is useful to
use an unthreaded script to run the profiler over the code you&#8217;re interested in
measuring if at all possible.</p>
</div>
<dl class="class">
<dt id="hotshot.Profile">
<em class="property">class </em><tt class="descclassname">hotshot.</tt><tt class="descname">Profile</tt><big>(</big><em>logfile</em><span class="optional">[</span>, <em>lineevents</em><span class="optional">[</span>, <em>linetimings</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#hotshot.Profile" title="Permalink to this definition">¶</a></dt>
<dd><p>The profiler object. The argument <em>logfile</em> is the name of a log file to use for
logged profile data. The argument <em>lineevents</em> specifies whether to generate
events for every source line, or just on function call/return. It defaults to
<tt class="docutils literal"><span class="pre">0</span></tt> (only log function call/return). The argument <em>linetimings</em> specifies
whether to record timing information. It defaults to <tt class="docutils literal"><span class="pre">1</span></tt> (store timing
information).</p>
</dd></dl>

<div class="section" id="profile-objects">
<span id="hotshot-objects"></span><h2>26.5.1. Profile Objects<a class="headerlink" href="#profile-objects" title="Permalink to this headline">¶</a></h2>
<p>Profile objects have the following methods:</p>
<dl class="method">
<dt id="hotshot.Profile.addinfo">
<tt class="descclassname">Profile.</tt><tt class="descname">addinfo</tt><big>(</big><em>key</em>, <em>value</em><big>)</big><a class="headerlink" href="#hotshot.Profile.addinfo" title="Permalink to this definition">¶</a></dt>
<dd><p>Add an arbitrary labelled value to the profile output.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.close">
<tt class="descclassname">Profile.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Close the logfile and terminate the profiler.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.fileno">
<tt class="descclassname">Profile.</tt><tt class="descname">fileno</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.fileno" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the file descriptor of the profiler&#8217;s log file.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.run">
<tt class="descclassname">Profile.</tt><tt class="descname">run</tt><big>(</big><em>cmd</em><big>)</big><a class="headerlink" href="#hotshot.Profile.run" title="Permalink to this definition">¶</a></dt>
<dd><p>Profile an <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a>-compatible string in the script environment. The
globals from the <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><tt class="xref py py-mod docutils literal"><span class="pre">__main__</span></tt></a> module are used as both the globals and locals
for the script.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.runcall">
<tt class="descclassname">Profile.</tt><tt class="descname">runcall</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**keywords</em><big>)</big><a class="headerlink" href="#hotshot.Profile.runcall" title="Permalink to this definition">¶</a></dt>
<dd><p>Profile a single call of a callable. Additional positional and keyword arguments
may be passed along; the result of the call is returned, and exceptions are
allowed to propagate cleanly, while ensuring that profiling is disabled on the
way out.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.runctx">
<tt class="descclassname">Profile.</tt><tt class="descname">runctx</tt><big>(</big><em>cmd</em>, <em>globals</em>, <em>locals</em><big>)</big><a class="headerlink" href="#hotshot.Profile.runctx" title="Permalink to this definition">¶</a></dt>
<dd><p>Evaluate an <a class="reference internal" href="../reference/simple_stmts.html#exec"><tt class="xref std std-keyword docutils literal"><span class="pre">exec</span></tt></a>-compatible string in a specific environment. The
string is compiled before profiling begins.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.start">
<tt class="descclassname">Profile.</tt><tt class="descname">start</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.start" title="Permalink to this definition">¶</a></dt>
<dd><p>Start the profiler.</p>
</dd></dl>

<dl class="method">
<dt id="hotshot.Profile.stop">
<tt class="descclassname">Profile.</tt><tt class="descname">stop</tt><big>(</big><big>)</big><a class="headerlink" href="#hotshot.Profile.stop" title="Permalink to this definition">¶</a></dt>
<dd><p>Stop the profiler.</p>
</dd></dl>

</div>
<div class="section" id="module-hotshot.stats">
<span id="using-hotshot-data"></span><h2>26.5.2. Using hotshot data<a class="headerlink" href="#module-hotshot.stats" title="Permalink to this headline">¶</a></h2>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.</span></p>
<p>This module loads hotshot profiling data into the standard <a class="reference internal" href="profile.html#module-pstats" title="pstats: Statistics object for use with the profiler."><tt class="xref py py-mod docutils literal"><span class="pre">pstats</span></tt></a> Stats
objects.</p>
<dl class="function">
<dt id="hotshot.stats.load">
<tt class="descclassname">hotshot.stats.</tt><tt class="descname">load</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#hotshot.stats.load" title="Permalink to this definition">¶</a></dt>
<dd><p>Load hotshot data from <em>filename</em>. Returns an instance of the
<a class="reference internal" href="profile.html#pstats.Stats" title="pstats.Stats"><tt class="xref py py-class docutils literal"><span class="pre">pstats.Stats</span></tt></a> class.</p>
</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><tt class="xref py py-mod docutils literal"><span class="pre">profile</span></tt></a></dt>
<dd>The <a class="reference internal" href="profile.html#module-profile" title="profile: Python source profiler."><tt class="xref py py-mod docutils literal"><span class="pre">profile</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Stats</span></tt> class</dd>
</dl>
</div>
</div>
<div class="section" id="example-usage">
<span id="hotshot-example"></span><h2>26.5.3. Example Usage<a class="headerlink" href="#example-usage" title="Permalink to this headline">¶</a></h2>
<p>Note that this example runs the Python &#8220;benchmark&#8221; pystones.  It can take some
time to run, and will produce large output files.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">hotshot</span><span class="o">,</span> <span class="nn">hotshot.stats</span><span class="o">,</span> <span class="nn">test.pystone</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prof</span> <span class="o">=</span> <span class="n">hotshot</span><span class="o">.</span><span class="n">Profile</span><span class="p">(</span><span class="s">&quot;stones.prof&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">benchtime</span><span class="p">,</span> <span class="n">stones</span> <span class="o">=</span> <span class="n">prof</span><span class="o">.</span><span class="n">runcall</span><span class="p">(</span><span class="n">test</span><span class="o">.</span><span class="n">pystone</span><span class="o">.</span><span class="n">pystones</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prof</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span> <span class="o">=</span> <span class="n">hotshot</span><span class="o">.</span><span class="n">stats</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s">&quot;stones.prof&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="o">.</span><span class="n">strip_dirs</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="o">.</span><span class="n">sort_stats</span><span class="p">(</span><span class="s">&#39;time&#39;</span><span class="p">,</span> <span class="s">&#39;calls&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">stats</span><span class="o">.</span><span class="n">print_stats</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">         850004 function calls in 10.090 CPU seconds</span>

<span class="go">   Ordered by: internal time, call count</span>

<span class="go">   ncalls  tottime  percall  cumtime  percall filename:lineno(function)</span>
<span class="go">        1    3.295    3.295   10.090   10.090 pystone.py:79(Proc0)</span>
<span class="go">   150000    1.315    0.000    1.315    0.000 pystone.py:203(Proc7)</span>
<span class="go">    50000    1.313    0.000    1.463    0.000 pystone.py:229(Func2)</span>
<span class="go"> .</span>
<span class="go"> .</span>
<span class="go"> .</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">26.5. <tt class="docutils literal"><span class="pre">hotshot</span></tt> &#8212; High performance logging profiler</a><ul>
<li><a class="reference internal" href="#profile-objects">26.5.1. Profile Objects</a></li>
<li><a class="reference internal" href="#module-hotshot.stats">26.5.2. Using hotshot data</a></li>
<li><a class="reference internal" href="#example-usage">26.5.3. Example Usage</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="profile.html"
                        title="previous chapter">26.4. The Python Profilers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="timeit.html"
                        title="next chapter">26.6. <tt class="docutils literal"><span class="pre">timeit</span></tt> &#8212; Measure execution time of small code snippets</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/hotshot.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="timeit.html" title="26.6. timeit — Measure execution time of small code snippets"
             >next</a> |</li>
        <li class="right" >
          <a href="profile.html" title="26.4. The Python Profilers"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="debug.html" >26. Debugging and Profiling</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2014, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 29, 2014.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>